﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Common;
using Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Serilog;
using RestSharp;
using Xhelper.ApiGate;

namespace Web.Controllers
{
  /// <summary>
  /// 控制器基类
  /// </summary>
  public class BaseController : Controller
  {
    protected IMemoryCache MemoryCache { get; set; }
    protected readonly ILogger<BaseController> _logger;

    protected string testUnitId { get; set; }  //当前用户所属的检测单位编码
    protected string uname { get; set; }       //当前用户名称
    protected string uid { get; set; }         //当前用户id
    protected string regCode { get; set; }     //当前用户所属机构编号


    protected Serilog.ILogger Log
    {
      get { return Serilog.Log.Logger; }
    }

    public BaseController() { }

    public BaseController(IMemoryCache memoryCache) { MemoryCache = memoryCache; }

    public BaseController(ILogger<BaseController> logger)
    {
      _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
      if (HttpContext != null)
      {
        uname = HttpContext.User.Claims.FirstOrDefault(x => x.Type.Equals(ClaimTypes.Name))?.Value;
        uid = HttpContext.User.Claims.FirstOrDefault(x => x.Type.Equals("uid"))?.Value;
        regCode = HttpContext.User.Claims.FirstOrDefault(x => x.Type.Equals("regCode"))?.Value;

        testUnitId = Request.Headers["testUnitId"];

        var isDebug = Request.Query["isDebug"] == "1";
        if (isDebug)
        {
        }
      }
    }

    //private void InitViewBag()
    //{
    //  ViewBag.NotBack = false;
    //}

  }
}
